package com.epic.rest;

import com.epic.inter.IJobManager;
import com.epic.po.quartz.TaskInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.annotation.Resource;

@Path("/v1/job")
@Component("QuartzResource")
public class QuartzResource {
    private static Logger logger =  LoggerFactory.getLogger(QuartzResource.class);

    @DubboReference(check = false)
    public IJobManager iJobManager;


    @POST
    @Path("add")
    @Produces("application/json;charset=UTF-8")
    public String add(@Context HttpServletRequest request,
                      TaskInfo taskInfo){
        String ret = "sucess";
        try{
            iJobManager.addJob(taskInfo);
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return  ret;
    }

    @GET
    @Path("addTask")
    @Produces("application/json;charset=UTF-8")
    public String add(@Context HttpServletRequest request){
        String ret = "sucess";
        try{
            TaskInfo taskInfo = new TaskInfo();
            taskInfo.setRunArgs("11");
            taskInfo.setTaskId("11");
            taskInfo.setTaskBean("QuartzTask");
            taskInfo.setRunMode(1);
//            taskInfo.setScheduleParam("* */5 * * * ? *");
//            taskInfo.setScheduleParam("0 /1 * * * ?");
            taskInfo.setScheduleParam("*/30 * * * * ?");
            taskInfo.setTaskDesc("QuartzTask");
            taskInfo.setTaskUser(1);
            iJobManager.addJob(taskInfo);
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return  ret;
    }


    @GET
    @Path("test")
    @Produces("application/json;charset=UTF-8")
    @Consumes("application/json;charset=UTF-8")
    public String test(){
        String ret = "sucess";
        try{
            ret = iJobManager.test();
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return  ret;

    }
}

